<?php include("_header.phtml") ?>
<form method="get" action="asset">
    <div class="form-inline form-search">
        <div class="form-group">
            <label for="category">Category</label>
            <select id="category" name="category" class="form-control">
                <option value="">
                    Any
                </option>

                <?php foreach($categories as $key => $category) { ?>
                    <option value="<?php echo esc($category['id']) ?>" <?php if(isset($params['category']) && $category['id'] == $params['category']) echo 'selected=""'; ?>>
                        <?php echo esc($category['name']) ?>
                    </option>
                <?php } ?>
            </select>
        </div>
        <div class="form-group">
            <label>Support level</label><br class="visible-xs-block">
            <?php foreach($constants['support_level'] as $id => $level) if(is_int($id)) { ?>
                <label>
                    <input type="checkbox" name="support[<?php echo esc($level) ?>]" value="1"
                        <?php if(isset($params['support']) && (is_array($params['support']) ?
                            isset($params['support'][$level]) && $params['support'][$level] :
                            in_array($level, explode(' ', $params['support'])))
                        ) echo 'checked=""';
                        ?>>
                    <?php echo esc(ucfirst($level)) ?>
                </label>
            <?php } ?>
        </div>
        <div class="form-group">
            <label>Godot version</label>
            <select id="category" name="godot_version" class="form-control">
                <option value="">
                    Any
                </option>

                <?php foreach($constants['common_godot_versions'] as $version) if(substr_count($version, '.') != 2) { ?>
                    <option value="<?php echo esc($version) ?>" <?php if(isset($params['godot_version']) && $version == $params['godot_version']) echo 'selected=""'; ?>>
                        <?php echo esc(ucfirst(str_replace('_', ' ', $version))) ?>
                    </option>
                <?php } ?>
            </select>
        </div>
        <div class="form-group">
            <label for="sort">Order by</label>
            <select id="sort" name="sort" class="form-control">
                <?php if(!isset($params['sort'])) $params['sort'] = 'updated'; foreach(['cost', 'name', 'updated'] as $id => $order) { ?>
                    <option value="<?php echo esc($order) ?>" <?php if(isset($params['sort']) && $order == $params['sort']) echo 'selected=""'; ?>>
                        <?php echo esc(ucfirst($order)) ?>
                    </option>
                <?php } ?>
            </select>
            <label>
                <input type="checkbox" name="reverse" <?php if(isset($params['reverse'])) echo 'checked=""'; ?>>
                Reverse
            </label>
        </div>
        <?php if(isset($params['user'])) { ?>
            <div class="form-group bg-primary" style="padding:0 5px;">
                <label>
                    <input type="checkbox" name="user" value="<?php echo esc($params['user']) ?>" checked="">
                    Only by <?php echo esc($params['user']) ?>
                </label>
            </div>
        <?php } ?>
    </div>
    <div class="input-group">
        <input type="text" class="form-control" name="filter" placeholder="Search for..." value="<?php if(isset($params['filter'])) echo esc($params['filter']) ?>">
        <span class="input-group-btn">
            <button class="btn btn-default" type="submit">Search</button>
        </span>
    </div>
</form>
<br/>
<div class="row">
    <?php foreach($data['result'] as $i => $asset) { ?>
        <div class="col-md-6 asset-item">
            <div class="media">
                <div class="media-left">
                    <a href="<?php echo raw($basepath) . '/asset/' . url($asset['asset_id']) ?>" class="img-thumbnail">
                        <img class="media-object" src="<?php echo esc($asset['icon_url']) ?>" alt="<?php echo esc($asset['title']) ?>'s icon" width=80 height=80>
                    </a>
                </div>
                <div class="media-body">
                    <h4 class="media-heading">
                        <a href="<?php echo raw($basepath) . '/asset/' . url($asset['asset_id']) ?>"><?php echo esc($asset['title']) ?></a>
                        <small><?php echo esc($asset['version_string']) ?></small>
                        <br>
                        <span class="label label-primary"><?php echo esc($asset['category']) ?></span>
                        <span class="label label-danger"><?php echo esc(ucfirst(str_replace('_', ' ', $asset['godot_version']))) ?></span>
                        <span class="label label-<?php echo raw([
                            'official' => 'success',
                            'community' => 'info',
                            'testing' => 'default',
                        ][$asset['support_level']]) ?>"><?php echo esc(ucfirst($asset['support_level'])) ?></span>
                    </h4>

                    <p class="text-muted">
                        Submitted by user <a href="?user=<?php echo esc($asset['author']) ?>" title="Search assets by '<?php echo esc($asset['author']) ?>'"><?php echo esc($asset['author']) ?></a>;
                        <a href="?<?php echo esc(http_build_query([
                            'filter' => $asset['cost'] // isset($params['filter']) ? $params['filter'] . ' ' . $asset['cost'] : $asset['cost']
                        ] + $params)) ?>" title="Search assets with filter '<?php echo esc($asset['cost']) ?>'"><?php echo esc($asset['cost']) ?></a>;
                        <span class="nowrap"><?php echo esc(explode(" ", $asset['modify_date'])[0]) ?></span>
                    </p>
                </div>
            </div>
        </div>
    <?php } ?>
</div>
<?php include("_pagination.phtml") ?>
<a href="<?php echo raw($basepath) . '/asset/edit?' . raw($query) . '&asset=-1' ?>" class="btn btn-warning"><i class="glyphicon glyphicon-hourglass"></i> View Pending</a>
<?php include("_footer.phtml") ?>
